#include "reg.h"
#include  "mem.h"
#define MAX_INST 100

map<addr_t, Reg> Mem::mem;

void Mem::readMem(int regId, int offset, Reg &reg){
	addr_t addr;
	if (RegFile::reg[regId].isValue)
		addr = make_pair(0, RegFile::reg[regId].value + offset);
	else
		addr = make_pair(RegFile::reg[regId].coefficient * MAX_INST + RegFile::reg[regId].hash, 
							RegFile::reg[regId].delta + offset);
	map<addr_t, Reg>::iterator i = mem.find(addr);
	if (i == mem.end())
		mem[addr] = reg.init(++ hashCount);
	reg = mem[addr];
}

void Mem::writeMem(int regId, int offset, Reg &reg){
	addr_t addr;
	if (RegFile::reg[regId].isValue)
		addr = make_pair(0, RegFile::reg[regId].value + offset);
	else
		addr = make_pair(RegFile::reg[regId].coefficient * MAX_INST + RegFile::reg[regId].hash, 
							RegFile::reg[regId].delta + offset);
	mem[addr] = reg;
}

void Mem::init(){
	mem.clear();
}